package com.df.springmvc.workflow.deploy.services;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.common.bean.DhtmlGridBean;
import com.common.bean.DhtmlGridFieldBean;
import com.common.util.DhtmlGrid.DhtmlGridAttribuate;
import com.df.springmvc.supper.SupperServices;
import com.df.springmvc.workflow.deploy.dao.DeployManageDao;

/**
 * 本类只做数据运算处理
 * @author Turner
 */
@Service
public class DeployManageServices extends SupperServices{
	@Autowired
	DeployManageDao dao;
	
	public List<Object> findAllEmployees(){
		// TODO Auto-generated method stub
		return null;
	}
	public DhtmlGridBean query(DhtmlGridBean gridBean,HashMap<String,String> param_HashMap) throws Exception{
		// TODO Auto-generated method stub
		// CurrentUserBean userBean=CurrentUserBean.loadFromSession();
		Object[] attribute=new Object[]{};
		gridBean.cleanSessionXmlFile();
		DhtmlGridAttribuate attribuate=new DhtmlGridAttribuate();
		attribuate.addFieldBean(new DhtmlGridFieldBean("","",DhtmlGridFieldBean.TYPE_CH,"40"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("defKey","流程定义编号",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER));
		attribuate.addFieldBean(new DhtmlGridFieldBean("defName","流程定义名称",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER));
		attribuate.addFieldBean(new DhtmlGridFieldBean("defName","流程定义描述",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER));
		attribuate.addFieldBean(new DhtmlGridFieldBean("dedeploymentfId","部署编号",DhtmlGridFieldBean.TYPE_ED,"120"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("defVersion","部署版本号",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER));
		attribuate.addFieldBean(new DhtmlGridFieldBean("deployTime","部署时间",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER));
		attribuate.addFieldBean(new DhtmlGridFieldBean("deployStatus","状态",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER));
		/**
		 * 增加对应的业务代码
		 */
		ArrayList<List<Object>> list=null;
		gridBean.setAttribuate(attribuate);
		list=dao.query(param_HashMap,attribute,0,2000);
		for(int i=0;i < list.size();i++){
			gridBean.addData(list.get(i));
			if((i + 1) % gridBean.getPageSize() == 0){
				gridBean.createSessionXmlFile();
			}
		}
		gridBean.createSessionXmlFile();
		return gridBean;
	}
	@Override
	public Object add(HashMap<String,String> map) throws Exception{
		// 增加
		String fileId=map.get("fileId");
		String filePath=webPath + "cache/" + fileId;
		String fileName="";
		for(String fileName_:new File(filePath).list()){
			fileName=fileName_;
		}
		HashMap<String,Object> tmp=new HashMap<String,Object>();
		tmp.put("fileId",fileId);
		tmp.put("filePath",filePath + "/" + fileName);
		return dao.add(tmp);
	}
	@Override
	public Object upd(HashMap<String,String> map) throws Exception{
		return "";
	}
	@Override
	public Object del(HashMap<String,String> map) throws Exception{
		Object rs=dao.del(new HashMap<String,Object>(map));
		return rs;
	}
	@Override
	public HashMap<String,Object> view(String depId) throws Exception{
		// TODO Auto-generated method stub
		return dao.view(depId);
	}
	public File getPicPreview(String path,String webRoot) throws IOException{
		// TODO Auto-generated method stub
		if(StringUtils.isEmpty(path))
			return null;
		File file=new File(path);
		if(file.isFile() && "zip".equals(FilenameUtils.getExtension(path))){
			File dir=new File(webRoot + "/temp");
			if(!dir.exists()){
				dir.mkdirs();
			}
			File picPath=new File(dir,UUID.randomUUID().toString() + ".png");
			extPicFromZip(path,picPath.getAbsolutePath());
			System.out.println("==============>" + picPath);
			return picPath;
		}else{
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	private String extPicFromZip(String filePath,String picPath){
		try{
			// 获得zip信息
			ZipFile zip=new ZipFile(filePath);
			File pic=new File(picPath);
			Enumeration<ZipEntry> entries=(Enumeration<ZipEntry>)zip.entries();// 获取zip文件中的各条目（子文件）
			while(entries.hasMoreElements()){// 依次访问各条目
				ZipEntry ze=(ZipEntry)entries.nextElement();
				if(ze.getName().endsWith(".png")){
					InputStream in=zip.getInputStream(ze);
					FileOutputStream out=new FileOutputStream(pic);
					IOUtils.copy(in,out);
					return picPath;
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	public Object suspend(HashMap<String,String> hashMap) throws Exception{
		// TODO Auto-generated method stub
		ArrayList<String> t=new ArrayList<String>();
		System.out.println(hashMap);
		for(String key:hashMap.get("ids").split(",")){
			if(!t.contains(key)){
				t.add(key);
			}
		}
		for(String key:t){
			dao.suspend(key);
		}
		return "成功";
	}
	public Object active(HashMap<String,String> hashMap) throws Exception{
		// TODO Auto-generated method stub
		ArrayList<String> t=new ArrayList<String>();
		for(String key:hashMap.get("ids").split(",")){
			if(!t.contains(key)){
				t.add(key);
			}
		}
		for(String key:t){
			dao.active(key);
		}
		return "成功";
	}
	public Object queryFlowJson(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		Object rs=dao.queryFlowJson(paramMap);
		return rs;
	}
	public Object doConfig(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		String userId=getCurrentUserBean().getUserId();
		paramMap.put("userId",userId);
		Object rs=dao.updateFormUrl(paramMap);
		return rs;
	}
	public Object doAuto(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		String userId=getCurrentUserBean().getUserId();
		paramMap.put("userId",userId);
		Object rs=dao.queryFormLastInfo(paramMap);
		return rs;
	}
	public Object queryFormInfo(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		return dao.queryFormInfo(paramMap);
	}
}
